﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Handrise.Mobile.BasicModal.Camera;
using Handrise.Mobile.BasicModal.Audio;

namespace Handrise.CDCable.Mobile.Business
{
    /// <summary>
    /// 相关文件信息服务
    /// </summary>
    public class LinkFileService
    {

        private AudioService audioService = new AudioService();

        public LinkFileService()
        {
            audioService.OnPlaying += new PlayingEvent(audioService_OnPlaying);
            audioService.OnRecording += new RecordEvent(audioService_OnRecording);
        }

        public event PlayingHandler OnPlaying;
        public event RecordingHandler OnRecording;

        /// <summary>
        /// 通过关联ID获取对应的文件信息
        /// </summary>
        /// <param name="linkIDs">多个关联ID,用","分隔,如“'id1','id2','id3'”</param>
        /// <returns></returns>
        public List<LinkFile> GetLinkFileByLinkID(string linkIDs)
        {
            return AppLogic.dataService.Find<LinkFile>("LinkID in(" + linkIDs + ")", "");
        }

        /// <summary>
        /// 获取需要上报的文件
        /// </summary>
        /// <param name="linkIDs"></param>
        /// <returns></returns>
        public List<LinkFile> GetNeedReportLinkFileByLinkID(string linkIDs)
        {
            return AppLogic.dataService.Find<LinkFile>("LinkID in(" + linkIDs + ") and ReportState = '未上报'", "");
        }

        /// <summary>
        /// 通过关联ID和类型获取文件信息
        /// </summary>
        /// <param name="linkIDs"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public List<LinkFile> GetLinkFileByLinkIDAndType(string linkIDs, string type)
        {
            return AppLogic.dataService.Find<LinkFile>("LinkID in(" + linkIDs + ") and Type = '" + type + "'", "");
        }

        /// <summary>
        /// 更新关联文件信息
        /// </summary>
        /// <param name="linkFile"></param>
        public bool UpdateLinkFile(LinkFile linkFile)
        {
            return AppLogic.dataService.Update<LinkFile>(linkFile) == 1;
        }

        /// <summary>
        /// 添加一个关联文件
        /// </summary>
        /// <param name="linkFile"></param>
        /// <returns></returns>
        public bool AddLinkFile(LinkFile linkFile)
        {
            return AppLogic.dataService.Add<LinkFile>(linkFile) == 1;
        }

        /// <summary>
        /// 删除关联文件信息和文件
        /// </summary>
        /// <param name="linkIDs"></param>
        /// <returns></returns>
        public void DeleteLinkFileByLinkID(string linkIDs)
        {
            List<LinkFile> linkFiles = AppLogic.dataService.Find<LinkFile>("LinkID in(" + linkIDs + ")", "");
            foreach (LinkFile linkFile in linkFiles)
            {
                if (File.Exists(linkFile.FullFileName))
                {
                    try
                    {
                        File.Delete(linkFile.FullFileName);
                    }
                    catch (Exception e) 
                    {
                    }
                }
                AppLogic.dataService.Delete<LinkFile>(linkFile.LinkFileID);
            }
        }

        /// <summary>
        /// 删除关联文件信息及文件
        /// </summary>
        /// <param name="linkFileID"></param>
        public void DeleteLinkFile(string linkFileID)
        {
            LinkFile linkFile = AppLogic.dataService.GetEntityByKey<LinkFile>(linkFileID);
            if (linkFile == null)
                return;

            if (File.Exists(linkFile.FullFileName))
            {
                try
                {
                    GC.Collect();
                    File.Delete(linkFile.FullFileName);
                }
                catch (Exception e)
                {
                }
            }
            AppLogic.dataService.Delete<LinkFile>(linkFile.LinkFileID);
        }

        /// <summary>
        /// 拍摄一张照片
        /// </summary>
        /// <param name="linkID">关联ID</param>
        /// <returns></returns>
        public LinkFile TaskPicture(string linkID)
        {
            try
            {
                string fileName = Guid.NewGuid().ToString() + ".jpg";
                CameraService cameraService = new CameraService();
                string fullFilePath = cameraService.TakePicutes(AppLogic.GetLocalPath() + "\\Photo\\" + fileName);
                if (fullFilePath != "")
                {
                    LinkFile linkFile = new LinkFile();
                    linkFile.LinkID = linkID;
                    linkFile.LinkFileID = Guid.NewGuid().ToString();
                    linkFile.FileName = fileName;
                    linkFile.FilePath = AppLogic.GetLocalPath() + "\\Photo\\";
                    linkFile.FullFileName = fullFilePath;
                    linkFile.ReportState = "未上报";
                    linkFile.Title = "现场照片" + DateTime.Now.ToString("yyyyMMddHHmmss");
                    linkFile.Type = "jpg";
                    linkFile.UserName = AppLogic.LoginUser.UserName;
                    return linkFile;
                }

                return null;
            }
            catch (Exception)
            {
                
                return null;
            }

        }

        public string GetAudioState()
        {
            if (audioService.PlayState != AudioState.Init)
            {
                return audioService.PlayState.ToString();
            }
            else if (audioService.RecordState != AudioState.Init)
            {
                return audioService.RecordState.ToString();
            }
            else
            {
                return AudioState.Init.ToString();
            }
        }

        public LinkFile Record(string linkID)
        {
            string fileName = Guid.NewGuid().ToString() + ".wav";
            string fullFilePath = AppLogic.GetLocalPath() + "\\Audio\\" + fileName;

            LinkFile linkFile = new LinkFile();
            linkFile.LinkID = linkID;
            linkFile.LinkFileID = Guid.NewGuid().ToString();
            linkFile.FileName = fileName;
            linkFile.FilePath = AppLogic.GetLocalPath() + "\\Audio\\";
            linkFile.FullFileName = fullFilePath;
            linkFile.ReportState = "未上报";
            linkFile.Title = "现场录音" + DateTime.Now.ToString("yyyyMMddHHmmss");
            linkFile.Type = "wav";
            linkFile.UserName = AppLogic.LoginUser.UserName;
            audioService.StartRecord(linkFile.FullFileName);
            return linkFile;
        }

        void audioService_OnRecording(int recordSec, bool isStop)
        {
            if (OnRecording != null)
                OnRecording(recordSec, isStop);
        }

        public void PauseRecord()
        {
            audioService.PauseRecord();
        }

        public void RestartRecord()
        {
            audioService.ReStartRecord();
        }

        public void StopRecord()
        {
            audioService.StopRecord();
        }

        private int allSec = 0;
        public int Play(string fileName)
        {
            if (File.Exists(fileName))
            {
                allSec = audioService.StartPlay(fileName);
                return allSec;
            }
            else
                return 0;
            
        }

        void audioService_OnPlaying(int restSec,bool isStop)
        {
            int playSec = allSec - restSec;

            if (OnPlaying != null)
                OnPlaying(playSec, isStop);
        }

        public void PausePlay()
        {
            audioService.PausePlay();
        }

        public void RestartPlay()
        {
            audioService.ReStartPlay();
        }

        public void StopPlay()
        {
            audioService.StopPlay();
        }

        /// <summary>
        /// 获取所有的文件信息
        /// </summary>
        public List<LinkFile> GetAllLinkFile()
        {
            return AppLogic.dataService.Find<LinkFile>("", "");
        }


    }

    public delegate void PlayingHandler(int playSec,bool isStop);
    public delegate void RecordingHandler(int recordSec,bool isStop);
}
